﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Text;
using System.Collections.Generic;

public partial class Manage_System_FlagItem : System.Web.UI.Page
{
    protected string ActName = "添加";
    protected string ActFlag = "AddSave";
    protected KeRui.BLL.AdminFlag BllAdminFlag = new KeRui.BLL.AdminFlag();
    protected KeRui.Model.AdminFlag ModelAdminFlag = new KeRui.Model.AdminFlag();
    protected int flagtype = 0;
    protected int pId = PageRequest.GetInt("pId", 0);
    private ManageErrMsg nsb = new ManageErrMsg();
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//判断登录
        ManageHelper.CheckAdminPower("system_flag");
        ModelAdminFlag.ParentID = pId;
        flagtype = PageRequest.GetInt("ft", -1);
        ModelAdminFlag.FlagType = flagtype;

        switch (ManageHelper.PageAct())
        {
            case "addsave":
                AddSave();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            case "select":
                getSelectPbyFlagType();
                break;
            default: break;
        }
    }
    #region 添加
    private void AddSave()
    {
        ManageHelper.CheckAdminPower("system_flagadd");//权限
        ModelAdminFlag.FlagType = PageRequest.GetInt("FlagType", -1);
        if (ModelAdminFlag.FlagType == -1)
        {
            nsb.AddErr("没有得到模块类别。");
        }
        ModelAdminFlag.ParentID = PageRequest.GetInt("ParentID", 0);
        if (ModelAdminFlag.ParentID == -1)
        {
            nsb.AddErr("没有得到父级ID");
        }
        ModelAdminFlag.FlagName = PageRequest.GetString("FlagName");
        if (ModelAdminFlag.FlagName == "")
        {
            nsb.AddErr("请输入模块名称。");
        }
        ModelAdminFlag.Flag = PageRequest.GetString("Flag");
        if (ModelAdminFlag.Flag == "")
        {
            nsb.AddErr("请输入模块标记。");
        }
        ModelAdminFlag.AppUrl = PageRequest.GetString("AppUrl");
        ModelAdminFlag.Icon = PageRequest.GetString("Icon");
        ModelAdminFlag.FlagAction = PageRequest.GetString("FlagAction");
        ModelAdminFlag.IsOpen = PageRequest.GetInt("IsOpen", 0) == 0 ? false : true;
        ModelAdminFlag.OrderID = PageRequest.GetInt("OrderID", -1);
        ModelAdminFlag.IsNav = PageRequest.GetInt("IsNav", -1);
        ModelAdminFlag.FlagGroup = PageRequest.GetInt("FlagGroup", -1);
        if (ModelAdminFlag.OrderID == -1)
        {
            nsb.AddErr("请正确输入排序。");
        }
        nsb.ChkErr();
        if (BllAdminFlag.Add(ModelAdminFlag) < 0)
        {
            nsb.AddErr("对不起！已存在相同的模块标记。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_adminadd", "添加了“" + ModelAdminFlag.FlagName + "”模块。");//写日志
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"Flaglist.aspx?ft=" + ModelAdminFlag.FlagType + "&sucmsg=添加成功。\";</script>");
        }
    }
    #endregion

    #region 反显编辑页面
    private void Edit()
    {
        ActName = "修改";
        ActFlag = "editsave";
        int id = PageRequest.GetInt("id", -1);
        if (id == -1)
        {
            nsb.AddErr("error1。"); nsb.ChkErr();
        }
        ModelAdminFlag = BllAdminFlag.GetModel(id);
        if (ModelAdminFlag == null)
        {
            nsb.AddErr("对不起您想要修改的信息不存在。"); nsb.ChkErr();
        }
    }
    #endregion

    #region 修改
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_flagedit");//权限
        ModelAdminFlag.ID = PageRequest.GetInt("ID", -1);
        if (ModelAdminFlag.ID == -1)
        {
            nsb.AddErr("修改信息不存在。"); nsb.ChkErr();
        }
        ModelAdminFlag.FlagType = PageRequest.GetInt("FlagType", -1);
        if (ModelAdminFlag.FlagType == -1)
        {
            nsb.AddErr("没有得到模块类别。");
        }
        ModelAdminFlag.ParentID = PageRequest.GetInt("ParentID", 0);
        if (ModelAdminFlag.ParentID == -1)
        {
            nsb.AddErr("没有得到父级ID");
        }
        ModelAdminFlag.FlagName = PageRequest.GetString("FlagName");
        if (ModelAdminFlag.FlagName == "")
        {
            nsb.AddErr("请输入模块名称。");
        }
        ModelAdminFlag.Flag = PageRequest.GetString("Flag");
        if (ModelAdminFlag.Flag == "")
        {
            nsb.AddErr("请输入模块标记。");
        }
        ModelAdminFlag.AppUrl = PageRequest.GetString("AppUrl");
        ModelAdminFlag.Icon = PageRequest.GetString("Icon");
        ModelAdminFlag.FlagAction = PageRequest.GetString("FlagAction");
        ModelAdminFlag.IsOpen = PageRequest.GetInt("IsOpen", 0) == 0 ? false : true;
        ModelAdminFlag.OrderID = PageRequest.GetInt("OrderID", -1);
        ModelAdminFlag.IsNav = PageRequest.GetInt("IsNav", -1);
        ModelAdminFlag.FlagGroup = PageRequest.GetInt("FlagGroup", -1);
        if (ModelAdminFlag.OrderID == -1)
        {
            nsb.AddErr("请正确输入排序。");
        }
        nsb.ChkErr();
        if (BllAdminFlag.Update(ModelAdminFlag) < 0)
        {
            nsb.AddErr("对不起！已存在相同的模块标记。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_adminadd", "修改了“" + ModelAdminFlag.FlagName + "”模块。");//写日志
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"Flaglist.aspx?ft=" + ModelAdminFlag.FlagType + "&sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_flagdel");//权限
        int id = PageRequest.GetInt("ID", -1);
        int ft = PageRequest.GetInt("ft", -1);
        if (id == -1)
        {
            nsb.AddErr("对不起您所要删除的数据不存在。");
            nsb.ChkErr();
        }
        ModelAdminFlag = BllAdminFlag.GetModel(id);
        if (ModelAdminFlag == null)
        {
            nsb.AddErr("对不起你要删除的模块信息不存在。"); nsb.ChkErr();
        }
        if (BllAdminFlag.Delete(id) > 0)
        {
            ManageHelper.AddLog("system_admindel", "删除了“" + ModelAdminFlag.FlagName + "”模块。");//写日志
            Response.Write("<script type=\"text/javascript\">window.location.href=\"Flaglist.aspx?ft=" + ft + "&sucmsg=恭喜！删除成功。\";</script>");
        }
        else
        {
            Response.Write("<script type=\"text/javascript\">window.location.href=\"Flaglist.aspx?ft=" + ft + "&errmsg=对不起删除失败！原因：该模块下存在子模块。\";</script>");
        }
    }
    #endregion

    #region 父级下拉列表
    int num = 0;
    protected string GetSelectP(int PId, int FlagType, int ParentID)
    {
        string xs = "";
        for (int i = 0; i < num; i++)
        {
            xs += "　";
        }
        StringBuilder RetStr = new StringBuilder();
        List<KeRui.Model.AdminFlag> listAdminFlag = BllAdminFlag.GetList(PId, FlagType);
        foreach (KeRui.Model.AdminFlag model in listAdminFlag)
        {
            if (ParentID == model.ID)
            {
                RetStr.Append("<option value=\"" + model.ID + "\" selected>" + xs + "" + model.FlagName + "</option>");
            }
            else
            {
                RetStr.Append("<option value=\"" + model.ID + "\">" + xs + "" + model.FlagName + "</option>");
            }
            if (model.HaveChildNav)
            {
                num++;
                RetStr.Append(GetSelectP(model.ID, FlagType, ParentID));
                num = 0;
            }
        }
        return RetStr.ToString();
    }

    private void getSelectPbyFlagType()////ajax
    {
        int FlagType = PageRequest.GetInt("Ft", -1);
        if (FlagType == -1)
        {
            Response.Write("error1"); Response.End();
        }
        StringBuilder RetStr = new StringBuilder();
        RetStr.Append("<select name=\"ParentID\">");
        RetStr.Append("<option >--请选择父级模块--</option>");
        RetStr.Append(GetSelectP(0, FlagType, -1));
        Response.Write(RetStr.ToString()); Response.End();
    }
    #endregion


}
